<?php
namespace app\api\controller;
use think\Controller;
use Qiniu\Auth;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;


class Uploadfile extends controller
{
	
      //js上传图片
    public function getfile(){

        $file = $_FILES['file'];

        $array = explode('.',$file['name']);

        $max_xb = count($array)-1;

        $hz = ".".$array[$max_xb];

        // 要上传图片的本地路径
        $filePath = $file['tmp_name'];
        // 上传到七牛后保存的文件名
        $key = date('YmdHis') . rand(0, 9999) . $hz;
        require_once APP_PATH . '/../vendor/Qiniu/autoload.php';
        // 需要填写你的 Access Key 和 Secret Key
        $accessKey = 'Xt30ohldZo5fsACro_8lRoseuLC-O51jIPRcM3yH';
        $secretKey = 'fRATa6a7RJrABGKEKdY9vQLx8fwk06qtlJv_wgs9';

        
        // 构建鉴权对象
        $auth = new Auth($accessKey, $secretKey);
        // 要上传的空间名
        $bucket = 'zjjt123';
        //外链域名
        $domain = 'http://zjjtoss.ccicgdauto.com';
        $token = $auth->uploadToken($bucket);
        $uploadMrg = new UploadManager();
        list($ret, $err) = $uploadMrg->putFile($token, $key, $filePath);
        if ($err !== null) {
            echo json_encode(array('status' => 0, 'err' => '上传失败!'));
            exit();
        } else {
            echo json_encode(array('status' => 1, 'data' => $domain .'/'. $ret['key']));
            exit();
        }
        
      
    }

}